package com.luotf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luotf.model.Bloger;
import com.luotf.model.Comments;
import com.luotf.service.BlogerService;
import com.luotf.service.CommentsService;

@Controller
public class CommentsController {

	@Autowired
	private CommentsService commentsservice;
	
	@Autowired
	private BlogerService blogerService;
	
	/**
	 * 
	 *@作者:
	 *@简介:获得文章的所有评论
	 * @param id
	 * @return
	 */
	@RequestMapping(value = { "/queryComments" },method=RequestMethod.POST)
	@ResponseBody
	public List<Comments> queryCommentsByBlogid(Integer id) {
		List<Comments> commentarr = commentsservice.queryByBlogid(id);
		return commentarr;
	}

	/**
	 * 
	 *@作者:
	 *@简介:插入评论
	 * @param comments
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/inserComment",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertComment(@RequestBody Comments comments,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		Bloger bloger = (Bloger)session.getAttribute("bloger");
		Bloger tobloger = blogerService.selectByPrimaryKey(comments.getTotBlogerId());
		
		if(bloger == null) {
			map.put("status", 4000);
			map.put("message", "用户没有登录");
			return map;
		}
		
		//获得谁写的评论
		comments.settBlogerId(bloger.getId());
		//插入评论然后返回受影响的行
		Integer row = commentsservice.insertComment(comments);
		
		if(row.equals(1)&&row !=null) {
			map.put("status", 2000);
			map.put("message", "评论成功");
			comments.setBloger(bloger);
			comments.setTobloger(tobloger);
			map.put("resultobj", comments);
			return map;
		}else {
			map.put("status", 5000);
			map.put("message", "评论失败");
			map.put("resultobj", null);
			return map;
		}
	}
	
	@RequestMapping(value="/delcomment",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> deletecomment(Integer delindex,Integer author,HttpSession session){
		
		System.out.println(delindex+"  "+author);
		Map<String,Object> map = new HashMap<String,Object>();
		Bloger bloger = (Bloger)session.getAttribute("bloger");
		//判断有没有登录
		if(bloger == null) {
			map.put("status", 4000);
			map.put("message", "用户没有登录");
			return map;
		}
		
		//判断有没有删除权限
		if(!(author.equals(bloger.getId()))) {
			map.put("status", 4005);
			map.put("message", "没有删除权限");
			return map;
		}
		
		Integer row = commentsservice.deletecomment(delindex);
		System.out.println(row);
		if(row != null || !(Integer.valueOf(0).equals(row))) {
			map.put("status", 2000);
			map.put("message", "删除成功");
			return map;
		}else {
			map.put("status", 5000);
			map.put("message", "删除失败");
			return map;
		}
	}
	

}
